package com.harmay.center.monitor.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.harmay.center.monitor.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author Zuper
 * @version 1.0
 * @date 2022/6/29 9:38
 * @description 用于alertmanager端webhook推送请求
 */
@RestController
@RequestMapping("/v1/alter")
@Slf4j
public class AlterFiringController {

    private ObjectMapper jackson = JsonUtils.jackson();

    @RequestMapping(value = "/handle", method = {RequestMethod.POST, RequestMethod.GET})
    public void alterMesHandle(HttpServletRequest request) throws JsonProcessingException {
        String param = null;
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);
            log.info("message info {}", jackson.writeValueAsString(responseStrBuilder.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
